/**
 * 全部评价
 * @author Qasim <15750783791@163.com>
 * @version 2016/11/17 1.0
 */
document.addEventListener('plusready' , ready ,false);
var self = memberInfo = '';
function ready(){
	self = plus.webview.currentWebview();
	memberInfo = plus.storage.getItem('memberInfo');
	memberInfo = $.parseJSON(memberInfo);
	$('.pic').attr('src' , getImgUrl(self.litpic , '../public/img/book.jpg'))
	$('.book_name').text(self.bookname);
	$('.author').text(self.author);
	console.log(JSON.stringify(self.res))
	if(self.res.comments != ''){
		$.each(self.res.comments, function(k , v) {
			var html = '<ul class="mui-table-view booklists list'+v.id+'" data-id="'+v.id+'" data-level="'+v.level+'" data-time="'+v.addtime+'" data-uid="'+v.uid+'"><li class="mui-table-view-cell mui-media">';
			html += '<a href="javascript:;"><img class="mui-media-object touxiang mui-pull-left" src="'+getImgUrl(v.avatar)+'">';
			html += '<div class="mui-media-body"><span class="mui-h5  mui-lan nickname">'+v.nickname+'</span>';
			html += '<span class="mui-pull-right mui-h6">'; 
			html += '<i class="iconfont mui-icon" '+(v.is_goods ? 'style="color:red" data-isGoods="'+v.is_goods+'" ':'')+'>&#xe61d; </i>';
			html += '<span class="goods">'+v.goods+'</span></span>';
			html += '<span class="iconfont gray mlr"><map class="mui-lvs">';
			for(var i = 1 ; i <= v.level ; i++){
				html += '&#xe627;'; 
			}   
			html += '</map></span><p class="words">'+v.content+'</p><p class="time"><span class="replysNum">'+v.number+'</span> 回复</p></div></a></li></ul>';
			$('.comment_lists').append(html);
		});
		replyDetails();
	}else{
		$('.comment_lists').html('<ul class="mui-table-view"><li class="mui-table-view-cell mui-media">暂无评价</li></ul>');
	}
	
}

/**
 * 加载更多
 * @param {Int} page [下一页]
 * @param {Object} isEnable [元素节点]
 */
function loadMore(page , isEnable){
	$.ajax({ 
		type : 'post',
		url : apiRoot + '/Home/Article/comments/',
		data : { 
			id : self.aid,
			uid : memberInfo.aid,
			page:page
		},
		beforeSend:function(){
			plus.nativeUI.showWaiting('请稍候...');
		},
		complete:function(){
			plus.nativeUI.closeWaiting();
		},
		dataType:'json',
		success : function(data){ 
 			if(data.comments != ''){  
				$.each(data.comments , function(k , v){  
					var html = '<ul class="mui-table-view booklists list'+v.id+'" data-id="'+v.id+'" data-level="'+v.level+'" data-time="'+v.addtime+'" data-uid="'+v.uid+'"><li class="mui-table-view-cell mui-media">';
						html += '<a href="javascript:;"><img class="mui-media-object touxiang mui-pull-left" src="'+getImgUrl(v.avatar)+'">';
						html += '<div class="mui-media-body"><span class="mui-h5  mui-lan nickname">'+v.nickname+'</span>';
						html += '<span class="mui-pull-right mui-h6">'; 
						html += '<i class="iconfont mui-icon" '+(v.is_goods ? 'style="color:red" data-isGoods="'+v.is_goods+'" ':'')+'>&#xe61d; </i>';
						html += '<span class="goods">'+v.goods+'</span></span>';
						html += '<span class="iconfont gray mlr"><map class="mui-lvs">';
						for(var i = 1 ; i <= v.level ; i++){
							html += '&#xe627;'; 
						}   
						html += '</map></span><p class="words">'+v.content+'</p><p class="time"><span class="replysNum">'+v.number+'</span> 回复</p></div></a></li></ul>';
					$('.comment_lists').append(html);
				})
				replyDetails();
				if(isEnable)isEnable.endPullUpToRefresh(false);
			}else{
				toast('没有更多的数据了'); 
				if(isEnable)isEnable.endPullUpToRefresh(true); 
			}
		},
		error : function(e){
			errorPrompt(e);
		}
	})
}



function replyDetails(){
	$('.booklists').unbind();
	$('.booklists').on('click' , function(){
		var paramater = {
			page :self.id,
			bookid : self.aid,
			aid:$(this).attr('data-id') , 
			avatar:$(this).find('img').attr('src'),
			nickname:$(this).find('.nickname').text(),
			title : $(this).find('.words').text(),
			goods:$(this).find('.goods').text(),
			level:$(this).attr('data-level'),
			addtime:$(this).attr('data-time'),
			uid : $(this).attr('data-uid'),
			is_goods:$(this).find('.mui-icon').attr('data-isGoods'),
			is_second:true
		}
		goUrl('pingjia-detail.html' , 'pingjia' ,paramater); 
	})
}


/**
 * 监听子页面透传数据
 */ 
window.addEventListener('updateComments' , function(result){
	if(result.detail.type == 'good'){ 
		$('.list'+result.detail.aid).find('i').css('color' , 'red').attr('data-isGoods' , true);
		$('.list'+result.detail.aid).find('.goods').text(parseInt($('.list'+result.detail.aid).find('.goods').text()) +1);
	}else{
		$('.list'+result.detail.aid).find('.replysNum').text(parseInt($('.list'+result.detail.aid).find('.replysNum').text()) +1);
	}
	mui.fire(plus.webview.getWebviewById(self.pages) , 'addComment' , {})
})
